package com.example.discover.view

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.featuresdk.model.Book
import com.example.discover.viewmodel.BooksUiAction
import com.example.featuresdk.vo.Status
import com.example.featuresdk.vo.UiState

@Preview
@Composable
fun PreviewBooksScreen(){
    val books = listOf(Book("2000","Java","123"), Book("2021","Android","999"))
    BooksScreen(Status.Success,"Hello", books, object : BooksUiAction(){})
}

@Composable
fun BooksScreen(uiState : UiState<List<Book>>, operation: BooksUiAction){
    /* observeAsState()
     * Starts observing this LiveData and represents its values via State.
     *
     * Every time there would be new value posted into the LiveData the returned State will be updated
     * causing recomposition of every State.value usage.
     *
     * The inner observer will automatically be removed when this composable disposes
     * or the current LifecycleOwner moves to the Lifecycle.State.DESTROYED state.
     */
    //val books by bookViewModel.books.observeAsState(emptyList<Book>())

    /* collectAsState()
     * Collects values from this StateFlow and represents its latest value via State.
     * The StateFlow.value is used as an initial value.
     *
     * Every time there would be new value posted into the StateFlow
     * the returned State will be updated causing recomposition of every State.value usage.
     */
    val books by uiState.data.collectAsState(initial = emptyList())

    BooksScreen(uiState.status, uiState.message, books, operation)
}

@Composable
fun BooksScreen(status: Status, message: String? = null, books: List<Book>, operation: BooksUiAction) {
    Column() {

        when (status) {
            Status.Loading -> Text("Loading....")
            Status.Error -> Text(message!!)
            Status.Success -> Text("Success^_^")
        }

        BookSummary(books)

        Spacer(Modifier.height(8.dp))

        BookList(books)

        Spacer(Modifier.height(8.dp))

        if(books.isEmpty()) {
            BookEditForm(operation = operation)
        } else {
            BookEditForm(books.last(), operation)
        }
    }
}

@SuppressLint("UnrememberedMutableState")
@Composable
fun BookEditForm(book: Book = Book(), operation: BooksUiAction){

    Column() {

        Row (modifier = Modifier.padding(all = 8.dp)){

            Button(onClick = {
                operation.create(book)
            }) {
                Text("Add")
            }

            Spacer(modifier = Modifier.width(20.dp))

            Button(onClick = {
                operation.update(Book(book.isbn,book.name,book.price))
//                operation.update(Book("2021061403X","Test-update","200"))
            }) {
                Text("Update")
            }

            Spacer(modifier = Modifier.width(20.dp))

            Button(onClick = {
                operation.delete(book.isbn)
            }) {
                Text("Remove")
            }
        }

        BookTextField("ISBN",
            mutableStateOf(book.isbn), KeyboardType.Ascii, _onValueChange = {book.isbn = it })

        Spacer(modifier = Modifier.height(5.dp))

        BookTextField("Name", mutableStateOf(book.name), _onValueChange = {book.name = it })

        Spacer(modifier = Modifier.height(5.dp))

        BookTextField("Price",
            mutableStateOf(book.price), KeyboardType.Number, _onValueChange = {book.price = it })

//        Spacer(modifier = Modifier.height(5.dp))
//
//        BookTextField("Date",mutableStateOf(book.date), KeyboardType.Number, _onValueChange = {book.date = it })
    }
}
